package com.funcity.stage.layers
{
	import com.greensock.TweenLite;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;

	public final class CloudLayer extends Sprite
	{
		private static const DENSITY_RATE:uint = 30000;
		private static const TWEEN_DURATION:uint = 2;
		
		private var _stageWidth:uint = 0;
		private var _stageHeight:uint = 0;
		private var _clouds:Vector.<BitmapData>;
		
		private var _maskerLayer:Sprite = new Sprite();
		private var _maskers:Vector.<Bitmap> = new Vector.<Bitmap>();
		
		public function CloudLayer(stageWidth:uint, stageHeight:uint, clouds:Vector.<BitmapData>)
		{
			super();

			mouseChildren = false;
			mouseEnabled = false;
			
			_stageWidth = stageWidth;
			_stageHeight = stageHeight;
			_clouds = clouds;
			
			var density:uint = Math.floor(stageWidth * stageHeight / DENSITY_RATE);
			while (density-- > 0)
			{
				var masker:Bitmap = new Bitmap();
				_maskerLayer.addChild(masker);
				_maskers.push(masker);
			}
			addChild(_maskerLayer);
		}

		public function drawCloudMasker(flying:Boolean):void
		{
			TweenLite.killTweensOf(_maskerLayer, false, { alpha:true });
			_maskerLayer.alpha = 0;
			
			var index:uint = flying ? _maskers.length : Math.floor((Math.random() * 0.2 + 0.3) * _maskers.length);
			var maskerAlpha:Number = flying ? 1 : Math.random() * 0.2 + 0.4;
			
			for (var idx:uint = 0; idx < _maskers.length; idx++)
			{
				var masker:Bitmap = _maskers[idx];
				
				masker.bitmapData = idx < index ? _clouds[index % _clouds.length] : null;
				masker.x = idx < index ? Math.random() * _stageWidth : 0;
				masker.y = idx < index ? Math.random() * _stageHeight : 0;
			}
			TweenLite.to(_maskerLayer, TWEEN_DURATION, { alpha:maskerAlpha });
		}
		
		public function drawCloud(flying:Boolean):void
		{
		}
	}
}